package com.pat.manager;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.pat.om.Contact;
import com.pat.utils.HibernateUtil;

public class ContactManager {

	public List get(String contactId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		String hql = "FROM Contact a WHERE a.contactId = :contact_id";
		Query query = session.createQuery(hql);
		query.setParameter("contact_id", contactId);
		List results = query.list();

		session.getTransaction().commit();

		return results;
	}

	public void post(String contactId, String friendCellNo) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		Contact contact = new Contact();
		contact.setContactId(contactId);
		contact.setFriendCellNo(friendCellNo);
		session.delete(contact);
		session.save(contact);

		session.getTransaction().commit();
	}

	public void post(Contact contact) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.save(contact);
		session.getTransaction().commit();
	}

	public int delete(String contactId, String friendCellNo) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		String hql = "delete from Contact a where a.contactId = :contact_Id and a.friendCellNo = :friend_CellNo";
		Query query = session.createQuery(hql);
		query.setParameter("contact_Id", contactId);
		query.setParameter("friend_CellNo", friendCellNo);
		int rowCount = query.executeUpdate();
		session.getTransaction().commit();
		return rowCount;

	}

	public int delete(Contact contact) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		String hql = "delete from Contact a where a.contactId = :contact_Id and a.friendCellNo = :friend_CellNo";
		Query query = session.createQuery(hql);
		query.setParameter("contact_Id", contact.getContactId());
		query.setParameter("friend_CellNo", contact.getFriendCellNo());
		int rowCount = query.executeUpdate();
		session.getTransaction().commit();
		return rowCount;
	}
}
